import Router from '@koa/router';
import { BeError, BeSuccess } from '../utils/response';
import MuseumLogin from '../middleware/museumLogin';
import { DeleteUserList, GetUserList } from '../services/museumUser';
import log from '../utils/log';

const router = new Router();

// 用户列表
router.get('/list', async function (ctx) {
  const { page, phoneNumber } = ctx.query;
  try {
    const data = await GetUserList(page, phoneNumber);
    ctx.body = BeSuccess(data);
  } catch (error) {
    console.log(error);
    ctx.body = BeError(error.message);
  }
});

// 添加用户
router.get('/add', async function (ctx) {
  try {
    const museumLogin = new MuseumLogin();
    await museumLogin.init();
    const url = await museumLogin.getQrCode();
    global.userManager.addTask(museumLogin);
    log.debug('【故宫】：添加用户');
    ctx.body = BeSuccess(url);
  } catch (error) {
    ctx.body = BeError(error.message);
  }
});

router.post('/del', async function (ctx) {
  const { uuid } = ctx.request.body;
  if (!uuid) {
    throw new Error('uuid为空');
  }

  try {
    const data = await DeleteUserList(uuid);
    ctx.body = BeSuccess(data);
  } catch (error) {
    console.log(error);
    ctx.body = BeError(error.message, 400);
  }
});

export default router.routes();
